package com.yolib.sanyi;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.yolib.sanyi.connection.ConnectionService;
import com.yolib.sanyi.connection.event.ConnectionEvent;
import com.yolib.sanyi.connection.event.EventHandler;
import com.yolib.sanyi.connection.event.NewsEvent;
import com.yolib.sanyi.connection.event.ServerConnectEvent;
import com.yolib.sanyi.object.NewsObject;
import com.yolib.sanyi.tool.Utility;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.widget.Toast;


public class LogoActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_logo);
		
		if(!Utility.detectNetWorkfunction(this))
			Toast.makeText(this, "無網路連線，請確認您的網路連線狀態", Toast.LENGTH_LONG).show();
		else {
			ServerConnectEvent mTestEvent = new ServerConnectEvent(this);
			mTestEvent.setHandler(mHandler);
			ConnectionService.getInstance().addAction(mTestEvent);
		}

	}
	
	EventHandler mHandler = new EventHandler() {

		@Override
		public void dispatchMessage(Message msg) {
			if(getClassName(msg).compareTo(ServerConnectEvent.class.getName()) == 0){
				if(msg.what == ConnectionEvent.SUCCESS) {
					new Thread() {

						@Override
						public void run() {
							try {
								Thread.sleep(3000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
							Intent mIntent = new Intent(LogoActivity.this, MainActivity.class);
							mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							LogoActivity.this.startActivity(mIntent);
							overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
							finish();
						}

					}.start();
				}
				else {
					Toast.makeText(LogoActivity.this, "伺服器忙碌中，請稍後再試", Toast.LENGTH_LONG).show();
				}
			}
		}
	};

}
